package com.terrace.web.tagext.easyui;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.terrace.web.tagext.easyui.model.JSFunction;
import com.terrace.web.tagext.easyui.utils.EasyUIUtils;
import com.terrace.web.tagext.easyui.support.DataGridTagSupport;

/**
 * 树形表格
 * @author jiangyg
 *
 */
public class TreeGridTag extends DataGridTagSupport {
	
	private static final long serialVersionUID = -8779115822670159860L;

	/**
	 * 定义树节点的字段
	 */
	private String treeField;
	
	/**
	 * 是否动画效果
	 */
	private boolean animate = true;
	
	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(TreeGridTag.class);
	
	public int doEndTag() throws JspException {
		try {
			JspWriter out = this.pageContext.getOut();
			out.print(this.bulidGrid());
		} catch (Exception e) {
			logger.error("生成easyui表格失败：", e);
		}
		return EVAL_PAGE;
	}

	@Override
	public String assembleExtendDataOptions() {
		StringBuffer sb = new StringBuffer();
		sb.append(this.transferExtensionForStr());
		EasyUIUtils.appendDatsOption(sb, "treeField", treeField);
		EasyUIUtils.appendDatsOption(sb, "animate", animate);
		EasyUIUtils.substringLastComma(sb);
		return sb.toString();
	}
	
	@Override
	public String createGridEvents() {
		StringBuffer sb = new StringBuffer();
		
		// 当数据加载成功时触发
		JSFunction onLoadSuccessFunc = new JSFunction();
		onLoadSuccessFunc.addFuncParams("row", "data");
		if (pagination) {
			onLoadSuccessFunc.addFuncBody("delete $(this)." + gridType() + "('options').queryParams['" + idField + "'];");
		}
		onLoadSuccessFunc.addFuncBody("$(\"#" + name + "\")." + gridType() + "(\"clearSelections\");");
		if (StringUtils.isNotBlank(onLoadSuccess)) {
			onLoadSuccessFunc.addFuncBody(onLoadSuccess + "(row, data);");
		}
		EasyUIUtils.appendJSObjectProperty(sb, "onLoadSuccess", onLoadSuccessFunc);
		
		// 当用户双击一行时触发
		// 参数包括： 	rowIndex：被双击行的索引，从 0 开始
		// 			rowData：被双击行对应的记录
		if (StringUtils.isNotBlank(onDblClick)) {
			JSFunction onDblClickFunc = new JSFunction();
			onDblClickFunc.addFuncParams("row");
			onDblClickFunc.addFuncBody(onDblClick + "(row);},");
			EasyUIUtils.appendJSObjectProperty(sb, "onDblClickRow", onDblClickFunc);
		}
		
		// 当用户单击一行时触发
		if (StringUtils.isNotBlank(onClick)) {
			JSFunction onClickFunc = new JSFunction();
			onClickFunc.addFuncParams("row");
			onClickFunc.addFuncBody(onClick + "(row);");
			EasyUIUtils.appendJSObjectProperty(sb, "onClickRow", onClickFunc);
		}
		EasyUIUtils.substringLastComma(sb);
		return sb.toString();
	}

	@Override
	public String gridType() {
		return "treegrid";
	}

	public String getTreeField() {
		return treeField;
	}

	public void setTreeField(String treeField) {
		this.treeField = treeField;
	}

	public boolean isAnimate() {
		return animate;
	}

	public void setAnimate(boolean animate) {
		this.animate = animate;
	}

}
